package com.example.mynewsweb.service;

import com.example.mynewsweb.dao.UserDao;
import com.example.mynewsweb.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Autowired
   private  UserDao userDao;
    @Override
    public User UserLogin(User user) {
        User userlog =userDao.findByID(user.getUserId());
        if(userlog==null){
            throw new RuntimeException("账号或密码错误");
        }else{
            if (userlog.getUserId().equals(user.getUserId())&&userlog.getUserPwd().equals(user.getUserPwd())){
                return userlog;
            }else{
                throw new RuntimeException("账号或密码错误!");
            }
        }

    }

    @Override
    public void applytoWriter(User user) {
        userDao.applytoWriter(user);
    }

    @Override
    public void updateUser1(User user) {
            userDao.updateUser(user);
    }

    @Override
    public void updateUser2(User user) {
        User usertest=userDao.findByName(user.getUserName());
        if (usertest==null){
            userDao.updateUser(user);
        }else{
            throw new RuntimeException("用户名已存在");
        }
    }

    @Override
    public void register(User user) {
        User userReg=userDao.findByName(user.getUserName());
        if (userReg==null){
                user.setUserKind("普通用户");
                userDao.save(user);
        }else{
            throw new RuntimeException("用户名已存在");
        }
    }

    @Override
    public User findByName(String userName) {
        return userDao.findByName(userName);
    }

    @Override
    public User findByID(String userId) {
        return userDao.findByID(userId);
    }


}
